package canss.webservice.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.beehive.controls.api.bean.Control;

import canss.supports.basic;
import canss.webservice.model.Feed;
import canss.webservice.service.PortalIntraWebServiceControl;


import canss.webservice.service.PortalIntraWebServiceControlBean;

/**
 * Servlet implementation class for Servlet: svtAjaxFeedlist
 *
 */
 public class svtAjaxFeedlist extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   
	private static final long serialVersionUID = 5944484567190253544L;

	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public svtAjaxFeedlist() {
		super();
	}   	 	
	
	@Control
	PortalIntraWebServiceControl control; //control = (control==null)?new PortalIntraWebServiceControlBean():control;
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		response.setContentType("text/html; charset=UTF-8");
		StringBuffer retstr = new StringBuffer();
		PrintWriter out = response.getWriter();
		
		try
		{
			String _pid = request.getParameter("_pid");
			String _mid = request.getParameter("_mid");
			String _notxml = request.getParameter("_notxml");
			
			control = (control==null)?new PortalIntraWebServiceControlBean():control;
			Feed[] feedByMultiple = control.getFeedByMultiple(_pid, _mid);

			if(_notxml!=null)
			{
				if(feedByMultiple==null || feedByMultiple.length==0)
					retstr.append("null");
				else
				{
					for(Feed feed : feedByMultiple)
					{
						retstr.append(feed.getTBLPID_PID()+","+feed.getTBLMID_MID()+","+feed.getFEEDURI()+"<br/>");
					}
				}
			}else
			{// in xml
				out.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
				weblogic.application.utils.XMLWriter writer = new weblogic.application.utils.XMLWriter(out);
				
				writer.addElement("canss");
					
				if(feedByMultiple==null || feedByMultiple.length==0)
					retstr.append("null");
				else
				{
					boolean ischanged = false;
					String lastpid = "";
					for(Feed feed : feedByMultiple)
					{
						if( !lastpid.equals(basic.var2str(feed.getTBLPID_PID())) )
						{
							lastpid = basic.var2str(feed.getTBLPID_PID());
							ischanged = true;							
						}
						
						if(ischanged)
						{
							writer.addElement("canssPerson:Person");
								writer.addElement("canssPerson:id", basic.var2str(feed.getTBLPID_PID()));
								writer.addElement("canssFeed:feedlist");
						}
									writer.addElement("canssFeed:feed");
										writer.addElement("canssTrace:ProviderModuleID", basic.var2str(feed.getTBLMID_MID()));
										writer.addElement("canssTrace:feeduri", basic.var2str(feed.getFEEDURI()));
										writer.addElement("canssTrace:ruleid", basic.var2str(feed.getRULEID()));
										writer.addElement("canssTrace:feedstatus", basic.var2str(feed.getFEEDSTATUS()));
									writer.closeElement();
						if(ischanged)
						{
								writer.closeElement();
							writer.closeElement();
						}
					}
				}
				
				writer.finish();
			}
			
		} catch (RuntimeException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			retstr.append(e.toString()+"<br/>");
		}
		out.write(retstr.toString());
	}   	  	    
}